<template>
  <div class="log">
    <basic-container>
      <h3 style="margin: 19px 0 37px 13px">校门时间组[{{this.$route.query.timePlanName}}]绑定人员详情
        <el-button type="primary" style="margin-left: 10px" @click="previous">返回上一页</el-button>
      </h3>
      <avue-crud
          ref="crud"
          :data="tableData"
          :option="tableOption"
          :page.sync="page"
          :permission="permissionList"
          :table-loading="tableLoading"
          @on-load="getList"
          @search-reset="searchReset"
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @size-change="sizeChange"
          @current-change="currentChange">
        <template slot="menu" slot-scope="scope">
          <el-button
              class="filter-item"
              icon="el-icon-edit"
              size="mini"
              type="primary"
              disabled
              style="margin-bottom: 4px"
              @click="controlHandleout(scope.row)">分配
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button
              class="filter-item"
              size="small"
              type="warning"
              disabled
              @click="batchAddHandleout()">批量分配校门时间组
          </el-button>
        </template>
      </avue-crud>

<!--校门时间分配弹窗-->
      <el-dialog
          :visible.sync="dialogEditVisibleout"
          title="校门时间分配"
          center
          width="54%" @close="dictItemVisible">
        <el-form>
          <el-form-item>
            <span>当前学生：{{ currentrow.sysDeptName }}&nbsp;{{ currentrow.personName }}&nbsp;学号：{{
                currentrow.number
              }}</span>
          </el-form-item>
          <el-form-item>
            <h4>当前时间方案</h4>
            <el-button v-if="currentrow.timePlanGateName === null" v-model="currentrow.timePlanGateName" type="info">未设置</el-button>
            <el-button v-else v-model="currentrow.timePlanGateName" type="primary">{{ currentrow.timePlanGateName }}</el-button>
          </el-form-item>
          <el-form-item :span="24">
            <h4>选择时间方案(单选)</h4>
            <el-radio-group v-model="schid" @change="changeScheme" style="width: 100%">
              <el-radio v-for="(item,index) in timePlanName" :key="item.id" :label="item.id" :value="item.timePlanName" border
                        style="text-align: left;margin: 4px 0 0 10px;width:23.5%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">{{ item.timePlanName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" :span="24">
          <el-button
              class="filter-item"
              size="small"
              type="primary"
              @click="handleUpdateout()">保存分配
          </el-button>
        </div>
      </el-dialog>
<!--批量分配校门弹窗-->
      <el-dialog
          :visible.sync="dialogbetchAddTimeout"
          center
          title="批量分配校门时间组"
          width="54%" @close="dictItemVisible">
        <el-form>
          <el-form-item>
            <p style="font-size: 16px;font-weight: bold;margin: 0 0 0 5px;color: black">
              当前筛选条件下共
              <span style="color: red">{{this.page.total}}</span>人</p>
          </el-form-item>
          <el-form-item :span="24">
            <h4>选择时间方案(单选)</h4>
            <el-radio-group v-model="schid" @change="changeScheme" style="width: 100%">
              <el-radio v-for="(item,index) in timePlanName" :key="item.id" :label="item.id" :span="6"
                        :value="item.timePlanName"
                        border size="medium"
                        style="text-align: left;margin: 4px 0 0 10px;width: 23.5%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">
                {{ item.timePlanName }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" :span="24">
          <el-button
              class="filter-item"
              size="small"
              type="primary"
              @click="batchSaveTimeout()">保存分配
          </el-button>
        </div>
      </el-dialog>
    </basic-container>
  </div>
</template>
<script>
import {mapGetters} from "vuex";
import {addScheduleObj, timegroupObj,TimeObj} from "@/api/pass/s-newtimegroup";
import {GROUP_SEX, PASS_DETAILS} from '@/const/Dict'
import {PERSON_DETAILS, PERSON_OPERA} from "@/const/personDict";

export default {
  name: "outSeeDetails",
  data() {
    return {
      dialogEditVisibleout:false,
      dialogbetchAddTimeout:false,
      tableData: [],
      searchForm: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      tableLoading: false,
      //单个分配
      timePlanName: [],
      currentrow: {},
      schid: '',
      chooseList: [],
      tableOption: {
        border: true,
        index: true,
        indexLabel: '序号',
        stripe: true,
        menuAlign: 'center',
        menuWidth: 120,
        menuType: 'button',
        align: 'center',
        emptyBtnText: '重置', //清空按钮文案
        refreshBtn: true,
        showClomnuBtn: false,
        searchSize: 'mini',
        searchMenuSpan: 6,
        // searchLabelWidth: 80,
        // searchSpan: 4,
        labelWidth: "20%",
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        selection: false,
        reserveSelection: true,
        props: {
          label: 'label',
          value: 'value'
        },
        column: [
          {
            label: '人员id',
            prop: 'id',
            hide: true
          },
          {
            label: '姓名',
            prop: 'personName',
            type: 'input',
            search: true,
          }, {
            label: '学号',
            prop: 'number',
            dicData: PASS_DETAILS,
          }, {
            label: '性别',
            prop: 'sex',
            type: 'select',
            search: true,
            dicData: GROUP_SEX,
            change: (value) => {
              this.searchForm.sex = value.value
              this.getList(this.page, this.searchForm)
            },
          },{
            label: '校区',
            prop: 'campusDeptArea',
            dicData: PERSON_DETAILS,
            editDisplay: false,
          },
          {
            label: '年级',
            prop: 'deptParentName',
            dicData: PASS_DETAILS,
          },
          {
            label: '年级',
            prop: 'deptId',
            search: true,
            type: 'select',
            hide: true,
            defaultExpandAll: true,
            dicUrl: '/admin/dept/PermissionTree',
            cascaderItem: ['deptName'],
            dicFormatter: (res) => {
              let gradeArr = res.data
              let value = []
              if(gradeArr.length> 0){
                for(let i of gradeArr){
                  if(i.typeNum == "2" && i.isLock === false){
                    value.push(i)
                  }
                }
                console.log(value)
                return value
              }
            },
            dicMethod: 'get',
            props: {
              label: 'name',
              value: 'id'
            },
            change: (value) => {
              this.searchForm.deptId = value.value
              this.getList(this.page, this.searchForm)
            },
          },

          {
            label: '班级',
            prop: 'sysDeptName',
            dicData: PASS_DETAILS,
          },
          {
            label: '班级',
            prop: 'deptName',
            search: true,
            filter: false,
            type: 'select',
            hide: true,
            defaultExpandAll: true,
            dicUrl: '/campus/sysdept/detpTree/{{key}}/3',
            dicFormatter: (res) => {
              return res.data[0].children;//返回字典的层级结构
            },
            dicMethod: 'get',
            props: {
              label: 'name',
              value: 'deptId',
            },
            change: (value) => {
              this.searchForm.deptId = value.value
              this.getList(this.page, this.searchForm)
            },
          },
          {
            label: '学生属性',
            prop: 'identityKey',
            search: true,
            type: 'select',
            dicUrl: '/campus/identity/selectAll?groupNum=2',
            dicMethod: 'get',
            props: {
              label: 'name',
              value: 'id'
            },
            change: (value) => {
              this.searchForm.identityKey = value.value
              this.getList(this.page, this.searchForm)
            },
          }, {
            label: '楼栋',
            prop: 'buildingName',
            dicData: PASS_DETAILS,
          },
            {
            label: '楼栋',
            prop: 'buildingId',
            hide: true,
            search: true,
            type: 'select',
            dicUrl: '/campus/building/selectAll',
            dicMethod: 'get',
            // searchClearable: false,
            props: {
              label: 'buildingName',
              value: 'buildingId'
            },
            change: (value) => {
              if (value.value === '') {
                return;
              }
              this.searchForm.buildingId = value.value
              this.getList(this.page, this.searchForm)
            },
          },
          {
            label: '宿舍号',
            prop: 'dormNumber',
            dicData: PASS_DETAILS,
            search: true,
            type: 'input',
          },
          {
            width: 150,
            label: '校门通行时间组',
            prop: 'timePlanGateName',
            dicData: PASS_DETAILS,
          },  {
            width: 150,
            label: '方案修改时间',
            prop: 'updateTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            addDisplay: false,
            editDisplay: false,
          }, {
            width: 100,
            label: '修改操作人',
            prop: 'operator',
            dicData: PERSON_OPERA,
          },
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permissions.sys_time_del, false),
      }
    }
  },
  methods: {
    getList(page, params) {
      let param = this.$route.query;
      this.tableLoading = true
      timegroupObj(Object.assign({
        timePlanGateId:param.id,
        current: page.currentPage,
        size: page.pageSize
      }, params,this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      })
    },

    // 校门
    controlHandleout(row) {
      this.dialogEditVisibleout = true
      this.currentrow = row
      let param = {
        "size": "999",
        timeType:0,
      }
      TimeObj(param).then(res => {
        if (res.data.code === 0) {
          this.timePlanName = res.data.data.records
          console.log(this.timePlanName)
        }
      })
    },
    //批量分配校门
    batchAddHandleout: function () {
      this.dialogbetchAddTimeout = true
      let param = {
        "size": "999",
        timeType:0,
      }
      TimeObj(param).then(res => {
        if (res.data.code === 0) {
          this.timePlanName = res.data.data.records
        }
      })
    },
    //批量分配保存
    batchSaveTimeout(params) {
      let that = this
      this.$confirm('确定为这些人员进行分配吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        // debugger
        return conditBatchTime(Object.assign({
          timePlanFrameConnectId: that.schid,
          timeType:0
        }, params, that.searchForm))
      }).then(res => {
        if (res.data.code === 0) {
          this.dialogbetchAddTimeout = false
          this.$message.success('分配成功')
        } else {
          this.dialogbetchAddTimeout = false
          this.$message.error('分配失败')
        }
        this.getList(this.page)
      })
    },

    // 选择要分配的方案
    changeScheme(value) {
      this.schid = value
    },

    //校门时间组分配
    handleUpdateout: function (row, index, done) {
      if (this.schid === "") {
        this.$message.error('请选择要分配的时间方案')
      } else {
        let param = {
          personId: this.currentrow.id,
          timePlanId: this.schid,
          timeType:0
        }
        addScheduleObj(param).then(res => {
          if (res.data.code === 0) {
            this.$message.success('分配成功')
          } else {
            this.$message.error('分配失败')
          }
          this.dialogEditVisibleout = false
          this.getList(this.page)
        })
      }
    },
    //关闭弹窗
    dictItemVisible: function () {
      this.dialogbetchAddTimeout = false
      this.dialogEditVisibleout = false
      this.schid = null
    },

    //返回
    previous: function () {
      this.editform = {}
      this.$router.go(-1)
    },

    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange(current) {
      this.page.currentPage = current
    },
    refreshChange() {
      this.getList(this.page)
    },
    searchReset() {
      this.searchForm = {};
      this.getList(this.page)
    }
  }
}
</script>

<style scoped>
h4 {
  margin: 0;
}
/deep/.el-form-item__label{
  text-align: left;
}
/deep/.avue-form__row{
  padding-left: 4px !important;
  padding-right: 36px!important;
}
/deep/.avue-form__menu--center{
  text-align: left;
}
/deep/.avue-form__menu--center .el-button{
  margin: 0 20px 0 0;
}
</style>
